package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.TeacherSignDao;
import cn.edu.sdjzu.xg.kcsj.domain.TeacherSign;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherSignService {
    private static TeacherSignService teacherSignService = null;
    private static TeacherSignDao teacherSignDao = TeacherSignDao.getInstance ();
    private TeacherSignService() {
    }
    public static TeacherSignService getInstance() {
        if (teacherSignService == null){
            teacherSignService = new TeacherSignService ();
        }
        return teacherSignService;
    }


    public Collection<TeacherSign> findAll () throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<TeacherSign> teacherSigns = null;
        teacherSigns = teacherSignDao.getInstance().findAll(connection,null,null);
        JdbcHelper.close(connection);
        return teacherSigns;
    }

    public TeacherSign find (int id_int) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        TeacherSign teacherSign = teacherSignDao.find(id_int,connection);
        JdbcHelper.close(connection);
        return teacherSign;
    }

    public boolean add (TeacherSign teacherSignToAdd) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加StudentSign
            added = teacherSignDao.add(teacherSignToAdd,connection);
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    public boolean update (TeacherSign teacherSignToUpdate) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            //增加StudentSign
            updated = teacherSignDao.update(teacherSignToUpdate,connection);
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated ;

    }

    public boolean delete (int id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            //增加用户
            deleted  = teacherSignDao.delete(id,connection);
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }
    public boolean deletes(List<TeacherSign> listToDelete) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            for(TeacherSign e:listToDelete) {
                int id= e.getId();
                //删除用户
                deleted = teacherSignDao.delete(id, connection);
            }
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }
}
